package com.bright.ghj.user.cas;

import com.bright.ghj.common.util.AssertUtil;
import com.bright.ghj.user.properties.CasProperties;
import com.bright.ghj.user.util.SecurityUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author txf
 * @Date 2022/6/24 15:54
 * @Description
 */
@Component
public class CasAuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    private CasProperties casProperties;

    public CasAuthenticationEntryPointImpl(CasProperties casProperties) {
        this.casProperties = casProperties;
    }

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String domain = SecurityUtil.getDomainOfRequestUrl(request);
        String serverUrl = SecurityUtil.getMatchedDomainUrl(casProperties.getAppServerUrl(), domain);
        AssertUtil.notNull(serverUrl, "未匹配到网址，无法跳转到send，" + domain);
        response.sendRedirect(serverUrl + "/send");
    }
}
